"""Bitbucket user page on Gittip. """ import decimal import os import re import requests from aspen import json, Response from gittip import AMOUNTS, CARDINALS, db from gittip.elsewhere import bitbucket from gittip.models import Participant # ========================================================================== ^L # Try to load from Bitbucket. # =========================== user_info = bitbucket.get_user_info(path['username']) # Try to load from Gittip. # ======================== # We can only tip Users, not Organizations (or whatever else type can be). username = user_info['username'] name = user_info.get('display_name') if not name: name = username # XXX Hack to work around our stringification of hstore values, which we do in # gittip/elsewhere/__init__.py. :-/ is_team = user_info.get("is_team", None) in (True, u"True") usertype = "unknown type of account" if is_team is None: can_tip = False img_src = "/assets/-/avatar-default.gif" elif not is_team: usertype = "user" account = bitbucket.BitbucketAccount(unicode(username), user_info) locked = account.is_locked lock_action = "unlock" if account.is_locked else "lock" if account.is_claimed: request.redirect('/%s/' % account.participant) participant = Participant.query.get(account.participant) if not user.ANON: my_tip = user.get_tip_to(participant.username) tip_or_pledge = "pledge" number = participant.get_number_of_backers() img_src = participant.get_img_src(128) elif is_team: usertype = "organization" bb_url = "https://bitbucket.org/api/2.0/users/%s/members" % username members = [] resp = requests.get(bb_url) if resp.status_code is not 200: raise Response(404, "Bitbucket is currently unavailable") else: bb_data = json.loads(resp.text) for m in bb_data['values']: members.append(m) while 'next' in bb_data: next_url = u"https://bitbucket.org%s" % bb_data['next'] resp = requests.get(next_url) bb_data = json.loads(resp.text) for m in bb_data['values']: members.append(m) usernames = [member['username'] for member in members] ON_GITTIP = """\ SELECT elsewhere.user_info -> 'username' AS username FROM elsewhere JOIN participants p ON p.username = elsewhere.participant WHERE elsewhere.platform = 'bitbucket' AND elsewhere.user_info -> 'username' = any(%s) AND p.claimed_time IS NOT NULL """ _on_gittip = [rec['username'] for rec in db.fetchall(ON_GITTIP, (usernames,))] on_gittip = [] not_on_gittip = [] for member in members: seq = on_gittip if member['username'] in _on_gittip else not_on_gittip tip_to = None if user.ANON else user.get_tip_to(member['username']) seq.append((member, tip_to)) can_tip = user.ANON number = len(members) img_src = user_info['avatar'] + "&s=128" url = "https://bitbucket.org/%s" % username title = username # ========================================================================== ^L {% extends templates/base.html %} {% block heading %}

Bitbucket

{% end %} {% block box %}

{{ escape(username) }} has

{{ number }}
{% if usertype == "user" %}
{{ 'person' if number == 1 else 'people' }} ready to give
{% elif usertype == "organization" %}
public member{{ '' if number == 1 else 's' }}
{% end %}
{% if user.ANON or usertype == "user" %} {% end %} {% end %} {% block page %} {% if usertype == "user" %}
{% if account.is_locked %}

{{ escape(username) }} has opted out of Gittip.

If you are {{ escape(username) }} on Bitbucket, you can unlock your account to allow people to pledge tips to you on Gittip.

{% if 0 %} {% end %} {% else %}

{{ escape(name) }} has not joined Gittip.

Is this you? {% if user.ANON %} {% if 0 %} Click here to opt in to Gittip. We never collect money for you until you do. {% end %} {% else %} Sign out and sign back in to claim this account {% end %}

{% if user.ANON %}

What is Gittip?

Gittip is a way to thank and support your favorite artists, musicians, writers, programmers, etc. by setting up a small weekly cash gift to them. Read more ...

Don't like what you see?

If you are {{ escape(username) }} you can explicitly opt out of Gittip by locking this account. We don't allow new pledges to locked accounts.

{% if 0 %} {% end %} {% end %} {% end %}
{% elif usertype == "organization" %} {% for i, sequence in enumerate([on_gittip, not_on_gittip]) %} {% set nsequence = len(sequence) %} {% if sequence %} {% end %} {% for member, tip in sequence %} {% else %} {% end %} {% end %} {% end %}

{{ nsequence }} {% if number > 0 %} ({{ "%.1f" % (nsequence * 100 / float(number)) }}%) {% end %} {{ 'is' if nsequence == 1 else 'are' }} {{ i == 0 and "also on" or "not on" }} Gittip

{% if not user.ANON %} {{ member['username'] }} {% for amount in AMOUNTS %} {% end %} {% if tip not in AMOUNTS %} old amount! {% end %} {{ member['username'] }}
{% else %}

Not sure what to do with {{ name }}.

I don't recognize the “{{ usertype }}” type of user on Bitbucket.
Sorry. :-( {% end %} {% end %}